/**   
 * Copyright © 2021 郑心怡. All rights reserved.
 * 
 * @Title:UserInfoServiceImpl.java 
 * @Prject: com.interesting.examsystem.user.service.impl
 * @Package: com.interesting.examsystem.user.service.impl
 * @author: ZhengXinyi   
 * @date: 2021-07-24
 * @version: V1.0   
 */
package com.interesting.examsystem.user.service.impl;


import java.util.List;
import java.util.Map.Entry;

import com.interesting.examsystem.common.exception.DuplicateUserNameException;
import com.interesting.examsystem.common.utils.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.interesting.examsystem.common.utils.BeanMapperUtils;
import com.interesting.examsystem.user.model.UserInfoDO;
import com.interesting.examsystem.user.mapper.UserInfoMapper;
import com.interesting.examsystem.user.service.IUserInfoService;
import com.interesting.examsystem.user.model.UserInfoDTO;
import com.interesting.examsystem.user.model.UserInfoVO;

/**
 * @ClassName: UserInfoServiceImpl
 * @Description:  服务接口实现
 * @author: ZhengXinyi
 * @date: 2021-07-24
 */
@Slf4j
@Transactional
@Service("userInfoService")
public class UserInfoServiceImpl implements IUserInfoService {
	@Autowired
	private UserInfoMapper userInfoMapper;
	
	@Override
	public Page<UserInfoVO> selectPageVo(Page<UserInfoDTO> page, UserInfoDTO userInfoDto) {
		OrderItem orderItem;
		if (!StrUtil.isEmpty(userInfoDto.getSortMapJson())) {
			JSONObject jsonObject = JSONUtil.parseObj(userInfoDto.getSortMapJson());
			for (Entry<String, Object> orderEntry : jsonObject.entrySet()) {
				orderItem = new OrderItem();
				orderItem.setAsc((boolean)orderEntry.getValue());
				orderItem.setColumn(orderEntry.getKey());
				page.addOrder(orderItem);
			}
		} else {
			orderItem = new OrderItem();
			orderItem.setAsc(true);
			orderItem.setColumn("SORT_NUMBER");
			page.addOrder(orderItem);
		}
		Page<UserInfoVO> resultPage = userInfoMapper.selectPageVo(page, userInfoDto);
		return resultPage;
	}
	
	@Override
	public UserInfoVO selectById(String id) {
		UserInfoVO userInfoVo = null;
		UserInfoDO userInfo = (UserInfoDO) userInfoMapper.selectById(id);
		if (ObjectUtil.isNotEmpty(userInfo)) {
			userInfoVo = BeanMapperUtils.map(userInfo, UserInfoVO.class);
		}
		return userInfoVo;
	}
	
	@Override
	public UserInfoVO selectOne(UserInfoDTO userInfoDto) {
		UserInfoVO userInfoVo = null;
		UserInfoDO entity = BeanMapperUtils.map(userInfoDto, UserInfoDO.class);
		QueryWrapper<UserInfoDO> queryWrapper = new QueryWrapper<>();
		queryWrapper.setEntity(entity);
		UserInfoDO userInfoDo = userInfoMapper.selectOne(queryWrapper);
		if (ObjectUtil.isNotEmpty(userInfoDo)) {
			userInfoVo = BeanMapperUtils.map(userInfoDo, UserInfoVO.class);
		}
		return userInfoVo;
	}
	
	@Override
	public int deleteBatchIds(List<String> ids) {
		return userInfoMapper.deleteBatchIds(ids);
	}
	
	@Override
	public int insert(UserInfoVO userInfoVo) throws DuplicateUserNameException {
		// 判断用户名是否已存在
		UserInfoDTO infoDO = new UserInfoDTO();
		infoDO.setUsername(userInfoVo.getUsername());
		UserInfoVO userInfoVO = this.selectOne(infoDO);
		if (ObjectUtil.isNotEmpty(userInfoVO)){
			throw new DuplicateUserNameException("用户名已存在");
		}
		userInfoVo.setId(IdUtil.objectId());
		userInfoVo.setPassword(EncryptUtil.encrypt(userInfoVo.getPassword()));
		UserInfoDO entity = BeanMapperUtils.map(userInfoVo, UserInfoDO.class);
		return userInfoMapper.insert(entity);
	}
	
	@Override
	public int update(UserInfoVO userInfoVo) {
		UpdateWrapper<UserInfoDO> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id", userInfoVo.getId());
		UserInfoDO entity = BeanMapperUtils.map(userInfoVo, UserInfoDO.class);
		entity.setPassword(EncryptUtil.encrypt(userInfoVo.getPassword()));
		return userInfoMapper.update(entity, updateWrapper);
	}
	
	@Override
	public void insertBatch(List<UserInfoVO> userInfoVos) {
		if (!ObjectUtil.isEmpty(userInfoVos)) {
			userInfoMapper.insertBatch(userInfoVos);
		}
	}

	/**
	 * 根据用户名称查询用户
	 *
	 * @param username
	 * @return
	 */
	@Override
	public UserInfoVO selectByName(String username) {
		UserInfoVO userInfoVO = null;
		UserInfoDO userInfoDO = userInfoMapper.selectByName(username);
		if (ObjectUtil.isNotEmpty(userInfoDO)) {
			userInfoVO = BeanMapperUtils.map(userInfoDO, UserInfoVO.class);
		}
		return userInfoVO;
	}
}
